// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#include "Rules/Rule.h"

#include "Rules/ActionRule.h"

namespace CangjieMigrator::Rules {

ActionRule::ActionRule(const TomlConfig &config)
    : replace(std::move(config["replace"])), comment(std::move(config["comment"]))
{}

std::vector<Actions::Action> ActionRule::GenerateAction(
    UpdateInstance::File &file, const ExtractedInfo &info, const Rule &rule) const
{
    auto result = std::vector<Actions::Action>();
    if (!replace.empty()) {
        auto actions = rule.matchRule->GenerateAction(file, info, replace, &rule);
        result.insert(result.end(), actions.begin(), actions.end());
    }
    if (!comment.empty()) {
        result.emplace_back(Actions::Comment(info.replacePosition, comment, &rule));
    }
    return result;
}

std::string ActionRule::ToString() const
{
    return "[ActionRule]\nReplace: " + replace + "\nComment: " + comment;
}

} // namespace CangjieMigrator::Rules
